
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/16/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yffeereimbur.dao;

import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.manager.commons.CommonUtil;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.application.yffeereimbur.entity.YfFeeReimbur;
import com.application.yffeereimbur.struts.form.YfFeeReimburForm;

/**
 * 
 * Description:YfFeeReimburDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/16/2012
 * 
 */
public class YfFeeReimburDAO extends HibernateGenericDao<YfFeeReimbur> {
	private static final Logger logger = Logger
			.getLogger(YfFeeReimburDAO.class);

	private YfFeeReimbur yfFeeReimbur;

	public YfFeeReimbur getYfFeeReimbur() {
		return this.yfFeeReimbur;
	}

	public void setYfFeeReimbur(YfFeeReimbur yfFeeReimbur) {
		this.yfFeeReimbur = yfFeeReimbur;
	}

	public Integer getAllCountYfFeeReimbur(YfFeeReimburForm yfFeeReimburForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT COUNT(0) FROM yf_fee_reimbur t left join yf_project p on t.project_no = p.project_no left join usertable u on t.operateid = u.id  WHERE 1=1 ");
		sql = getStringBuffer(yfFeeReimburForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}

	public void deleteYfFeeReimburs(YfFeeReimburForm yfFeeReimburForm) {
		String hql = " delete from YfFeeReimbur where id ='"
				+ yfFeeReimburForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfFeeReimburs failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfFeeReimbur(YfFeeReimburForm yfFeeReimburForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT t.*,u.rname rname,p.*  FROM yf_fee_reimbur t left join yf_project p on t.project_no = p.project_no  left join usertable u on t.operateid = u.id  WHERE 1=1 ");
		sql = getStringBuffer(yfFeeReimburForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}
	
	
	public List staticYfFeeReimbur(YfFeeReimburForm yfFeeReimburForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT t.*,u.rname rname,p.*  FROM yf_fee_reimbur t left join yf_project p on t.project_no = p.project_no  left join usertable u on t.operateid = u.id  WHERE 1=1 ");
		sql = getStringBuffer1(yfFeeReimburForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(YfFeeReimburForm yfFeeReimburForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfFeeReimburForm.getProjectNo().trim() + "%");
		}

		if (null != yfFeeReimburForm.getFeeType()
				&& yfFeeReimburForm.getFeeType().longValue() != 0) {
			buf.append(" and t.FEE_TYPE = ? ");
			args.add(yfFeeReimburForm.getFeeType());
		}

		if (null != yfFeeReimburForm.getAuditStatus()
				&& yfFeeReimburForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfFeeReimburForm.getAuditStatus());
		}

		if (StringUtils.isNotBlank(yfFeeReimburForm.getOrg())) {
			buf.append(" and t.ORG=? ");
			args.add(yfFeeReimburForm.getOrg().trim());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getOperateid())) {
			buf.append(" and t.OPERATEID=? ");
			args.add(yfFeeReimburForm.getOperateid().trim());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfFeeReimburForm.getId());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfFeeReimburForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfFeeReimburForm.getProjectOrg());
		}
		if (null != yfFeeReimburForm.getProjectType()
				&& yfFeeReimburForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfFeeReimburForm.getProjectType());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getRname())) {
			buf.append(" and u.rname like ? ");
			args.add("%" + yfFeeReimburForm.getRname().trim() + "%");
		}
		if (yfFeeReimburForm.getYear()!=null) {
			buf.append(" and year(t.apply_date) = ? ");
			args.add(yfFeeReimburForm.getYear());
		}
		if (StringUtils.isBlank(yfFeeReimburForm.getId())) {
			buf = CommonUtil.filterDataFee(buf,args);
		}
		
		return buf;
	}

	
	
	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer1(YfFeeReimburForm yfFeeReimburForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfFeeReimburForm.getProjectNo().trim() + "%");
		}

		if (null != yfFeeReimburForm.getFeeType()
				&& yfFeeReimburForm.getFeeType().longValue() != 0) {
			buf.append(" and t.FEE_TYPE = ? ");
			args.add(yfFeeReimburForm.getFeeType());
		}

		if (null != yfFeeReimburForm.getAuditStatus()
				&& yfFeeReimburForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfFeeReimburForm.getAuditStatus());
		}

		if (StringUtils.isNotBlank(yfFeeReimburForm.getOrg())) {
			buf.append(" and t.ORG=? ");
			args.add(yfFeeReimburForm.getOrg().trim());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getOperateid())) {
			buf.append(" and t.OPERATEID=? ");
			args.add(yfFeeReimburForm.getOperateid().trim());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfFeeReimburForm.getId());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfFeeReimburForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfFeeReimburForm.getProjectOrg());
		}
		if (null != yfFeeReimburForm.getProjectType()
				&& yfFeeReimburForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfFeeReimburForm.getProjectType());
		}
		if (StringUtils.isNotBlank(yfFeeReimburForm.getRname())) {
			buf.append(" and u.rname like ? ");
			args.add("%" + yfFeeReimburForm.getRname().trim() + "%");
		}
		if (yfFeeReimburForm.getYear()!=null) {
			buf.append(" and year(t.apply_date) = ? ");
			args.add(yfFeeReimburForm.getYear());
		}
		buf = CommonUtil.filterDataFeeStatic(buf,args);
		return buf;
	}
}
